<template>
    <div>
        <div class="page-title">编辑党建</div>
        <el-form ref="form" :model="form" label-width="100px">
            <el-form-item label="党建标题.">
                <el-input ref="title" v-model="form.title" placeholder="请输入党建标题"></el-input>
            </el-form-item>

            <el-form-item label="所属分类.">
                <el-select ref="type" v-model="form.tid" placeholder="-- 请选择分类 --">
                    <el-option v-for="i in options" :key="i" :label="i.name" :value="i.id"></el-option>
                </el-select>
            </el-form-item>

            <el-form-item label="封面照片.">
                <el-upload :action="uploadImage" :show-file-list="false" :before-upload="beforeUpload"
                    :on-success="uploadSuccess">
                    <div v-if="cover" class="relative">
                        <img :src="cover" class="article">
                        <el-button class="reupload" icon="el-icon-refresh" size="mini">重新上传</el-button>
                    </div>
                    <i v-else class="el-icon-plus"></i>
                    <template #tip>
                        <div class="el-upload__tip">
                            只能上传 PNG / JPG / GIF / BMP / WEBP 文件，且不超过 2MB，建议尺寸 415 * 300
                        </div>
                    </template>
                </el-upload>
            </el-form-item>

            <el-form-item label="党建简介.">
                <el-input ref="desc" v-model="form.desc" placeholder="请输入党建简介"></el-input>
            </el-form-item>

            <el-form-item label="党建内容.">
                <Editor :value="editorValue" @change="changeEditor"></Editor>
            </el-form-item>

            <el-form-item>
                <el-button type="success" icon="el-icon-check" @click="submit">修改</el-button>
                <el-button icon="el-icon-close" @click="cancel">取消</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
    import {
        get,
        put
    } from '@/network/request'
    import Editor from '@/components/Editor'
    import {
        __URL__
    } from '@/main'

    export default {
        name: 'EditParty',
        components: {
            Editor
        },
        data() {
            return {
                uploadImage: __URL__ + '/uploadImage', // 上传图片地址
                form: {
                    id: this.$route.params.id,
                    cid: sessionStorage.getItem('cid'),
                    title: '',
                    tid: '',
                    cover: '',
                    desc: '',
                    content: ''
                },
                options: [], // 分类数组
                cover: '',
                editorValue: '' // 富文本内容
            }
        },
        mounted() {
            this.$nextTick(() => {
                // 获取信息
                get('/getParty', {
                    id: this.$route.params.id,
                    cid: sessionStorage.getItem('cid')
                }).then(res => {
                    const result = res.data
                    if (result.code == 200) {
                        this.form.title = result.data.title
                        this.form.tid = (result.data.tid != 0) ? result.data.tid : ''
                        this.form.cover = result.data.cover
                        this.form.desc = result.data.desc
                        this.editorValue = result.data.content
                        this.cover = result.data.cover
                    } else {
                        this.$router.push('/404')
                    }
                }).catch()

                // 获取分类
                get('/selectPartyType', {
                    cid: sessionStorage.getItem('cid')
                }).then(res => {
                    const result = res.data
                    if (result.code == 200) {
                        this.options = result.data
                    }
                }).catch()
            })
        },
        methods: {
            // 富文本内容
            changeEditor(val) {
                this.form.content = val
            },
            // 上传准备
            beforeUpload(file) {
                if (['image/png', 'image/jpeg', 'image/gif', 'image/bmp', 'image/webp'].indexOf(file
                        .type) == -1) {
                    this.$message.error({
                        message: '请上传正确的图片格式！',
                        type: 'error',
                        offset: 70
                    })
                    return false
                }

                const size = file.size / 1024 / 1024 < 2
                if (!size) {
                    this.$message.error({
                        message: '图片大小不能超过 2MB！',
                        type: 'error',
                        offset: 70
                    })
                    return false
                }
            },
            // 上传成功
            uploadSuccess(res, file) {
                this.form.cover = res.data
                this.cover = URL.createObjectURL(file.raw)
            },
            // 提交
            submit() {
                if (this.form.title == '') {
                    this.$notify.info({
                        title: '系统提示',
                        message: '请输入党建标题',
                        offset: 70
                    })
                    this.$refs['title'].focus()
                    return false
                }

                if (this.form.type == '') {
                    this.$notify.info({
                        title: '系统提示',
                        message: '请选择分类',
                        offset: 70
                    })
                    this.$refs['type'].focus()
                    return false
                }

                if (this.form.cover == '') {
                    this.$notify.info({
                        title: '系统提示',
                        message: '请上传封面照片',
                        offset: 70
                    })
                    return false
                }

                if (this.form.desc == '') {
                    this.$notify.info({
                        title: '系统提示',
                        message: '请输入党建简介',
                        offset: 70
                    })
                    this.$refs['desc'].focus()
                    return false
                }

                if (this.form.content == '') {
                    this.$notify.info({
                        title: '系统提示',
                        message: '请输入党建内容',
                        offset: 70
                    })
                    return false
                }

                put('/modifyParty', this.form).then(res => {
                    const result = res.data
                    if (result.code == 200) {
                        this.$notify.success({
                            title: '系统提示',
                            message: '党建修改成功',
                            offset: 70
                        })
                        this.$router.push('/party')
                    }
                }).catch()
            },
            // 取消
            cancel() {
                this.$router.push('/party')
            }
        }
    }
</script>

<style>
</style>